package top.chenbn.guli.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.chenbn.guli.client.EduClient;
import top.chenbn.guli.client.UcenterClient;
import top.chenbn.guli.common.util.ordervo.CourseWebVoOrder;
import top.chenbn.guli.common.util.ordervo.UcenterMemberOrder;
import top.chenbn.guli.entity.Order;
import top.chenbn.guli.mapper.OrderMapper;
import top.chenbn.guli.service.OrderService;
import top.chenbn.guli.utils.OrderNoUtil;

/**
 * 订单 服务实现类
 *
 * @author chenbn
 * @since 2020-07-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
  @Autowired private EduClient eduClient;

  @Autowired private UcenterClient ucenterClient;

  // 1 生成订单的方法
  @Override
  public String createOrders(String courseId, String memberId) {
    // 通过远程调用根据用户id获取用户信息
    UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

    // 通过远程调用根据课程id获取课信息
    CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

    // 创建Order对象，向order对象里面设置需要数据
    Order order = new Order();
    order.setOrderNo(OrderNoUtil.getOrderNo()); // 订单号
    order.setCourseId(courseId); // 课程id
    order.setCourseTitle(courseInfoOrder.getTitle());
    order.setCourseCover(courseInfoOrder.getCover());
    order.setTeacherName(courseInfoOrder.getTeacherName());
    order.setTotalFee(courseInfoOrder.getPrice());
    order.setMemberId(memberId);
    order.setMobile(userInfoOrder.getMobile());
    order.setNickname(userInfoOrder.getNickname());
    order.setStatus(0); // 订单状态（0：未支付 1：已支付）
    order.setPayType(1); // 支付类型 ，微信1
    baseMapper.insert(order);
    // 返回订单号
    return order.getOrderNo();
  }
}
